package cn.com.nes.site.service.product.impl;

import cn.com.nes.site.service.product.ProductAttributeHandlerFactory;
import cn.com.nes.site.service.product.handle.ProductAttributeHandler;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 产品属性处理器工厂实现类
 */
@Service
public class ProductAttributeHandlerFactoryImpl implements ProductAttributeHandlerFactory {

    @Resource
    private List<ProductAttributeHandler> productAttributeHandlerList;

    private static Map<Integer, ProductAttributeHandler> productAttributeHandlerMap;

    @PostConstruct
    private void init() {
        productAttributeHandlerMap = productAttributeHandlerList.stream().collect(Collectors.toMap(ProductAttributeHandler::getNodeType, t -> t));
    }

    @Override
    public ProductAttributeHandler getProductAttributeHandler(Integer nodeType) {
        return productAttributeHandlerMap.get(nodeType);
    }
}
